package runtime;

import java.util.TreeMap;

import exception.TypeException;

import util.Type;

public class TypeFactory {
	
	private static TypeFactory instance = null;
	
	public static TypeFactory getInstance() {
		if(instance==null) {
			instance = new TypeFactory();
		}
		return instance;
	}
	
	private TreeMap<String, Type> typeMap;
	
	private TypeFactory() {
		typeMap = new TreeMap<String, Type>();
		typeMap.put("bool", new Type("bool", false));
		typeMap.put("int", new Type("int", false));
		typeMap.put("double", new Type("double", false));
		typeMap.put("char", new Type("char", false));
		typeMap.put("string", new Type("string", false));
	}
	
	public void addType(Type type) throws TypeException {
		if(typeMap.containsKey(type.getName())) {
			throw new TypeException();
		}
		typeMap.put(type.getName(), type);
	}
	
	public Type getType(String type) throws TypeException {
		if(!typeMap.containsKey(type)) {
			throw new TypeException();
		}
		return typeMap.get(type);
	}
}
